package cn.vshare.service.impl;

import cn.vshare.dao.ReplyDao;
import cn.vshare.entity.Comment;
import cn.vshare.entity.Reply;
import cn.vshare.entity.User;
import cn.vshare.exception.AlreadyLikeException;
import cn.vshare.exception.NotLikeException;
import cn.vshare.service.ReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @Auther: 张恒
 * @Date: 2018/9/18 16:24
 * @Description: service实现
 */
@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    private ReplyDao replyDao;

    @Override
    public PageInfo<Reply> queryAllReply(String commentId, Integer pageNum, Integer pageSize) {
        PageInfo pageInfo = null;
        if (commentId != null && !commentId.equals("")) {
            PageHelper.startPage(pageNum, pageSize, true);
            List<Reply> list = replyDao.queryAllReply(commentId);
            pageInfo = new PageInfo(list);
        }
        return pageInfo;
    }

    @Override
    public Reply addReply(User user, User toUser, String commentId, String context, String date) {
        Reply reply = new Reply();
        String replyId = UUID.randomUUID().toString();
        reply.setId(replyId);
        reply.setCreateTime(date);
        reply.setUser(user);
        reply.setToUser(toUser);
        Comment comment = new Comment();
        comment.setId(commentId);
        reply.setComment(comment);
        reply.setContext(context);
        if (replyDao.addReply(reply)) {
            return replyDao.queryReplyById(replyId);
        }
        return null;

    }

    @Override
    public boolean delReply(String replyId) {
        if (replyId != null && !replyId.equals("")) {
            return replyDao.delReply(replyId);
        }
        return false;
    }

    @Override
    public boolean updateReplyAuditStatus(String replyId, Integer auditStatus) {
        if (replyId != null && auditStatus != null && !replyId.equals("")) {
            return replyDao.updateReplyAuditStatus(replyId, auditStatus);
        }
        return false;
    }

    @Override
    public boolean updateReplyByInform(String replyId) {
        if (replyId != null && !replyId.equals("")) {
            return replyDao.updateReplyByInform(replyId);
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public boolean updateReplyAddLike(User user, String replyId) throws Exception {
        int ifOrNot = replyDao.queryReplyIfLike(replyId, user);
        if (ifOrNot == 0) {
            String id = UUID.randomUUID().toString();
            Reply reply = replyDao.queryReplyById(replyId);
            if (reply == null) {
                throw new Exception();
            } else {
                Integer parseCount = reply.getParseCount();
                if (replyDao.updateReplyAddLike(id, user, replyId)) {
                    parseCount += 1;
                    return replyDao.updateReplyParseCount(replyId, parseCount);
                }
            }
        } else {
            throw new AlreadyLikeException("已经点过赞");
        }


        return false;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public boolean updateReplyCancelLike(User user, String replyId) throws Exception {
        int ifOrNot = replyDao.queryReplyIfLike(replyId, user);
        if (ifOrNot == 1) {
            Reply reply = replyDao.queryReplyById(replyId);
            if (reply != null) {
                Integer parseCount = reply.getParseCount();
                if (replyDao.updateReplyCancelLike(user, replyId)) {
                    if (parseCount >= 1) {
                        parseCount -= 1;
                        return replyDao.updateReplyParseCount(replyId, parseCount);
                    }
                }
            } else {
                throw new Exception();
            }
        } else {
            throw new NotLikeException("没有点赞");
        }
        return false;
    }

    @Override
    public Reply queryReplyById(String replyId) {
        if (replyId != null && !replyId.equals("")) {
            return replyDao.queryReplyById(replyId);
        }
        return null;
    }

    @Override
    public int queryReplyIfLike(String replyId, User user) {
        return replyDao.queryReplyIfLike(replyId, user);
    }

    @Override
    public boolean updateReplyParseCount(String replyId, Integer parseCount) {
        return false;
    }
}
